package com.haoyeteng.sxbd.core.pagination;

import java.util.ArrayList;
import java.util.List;

/**
 * 列表分页。包含list属性。（后台分页样式）
 * @author 理想
 *
 * @param <T>
 */
public class Pagination<T> extends SimplePage {

	private static final long serialVersionUID = 1L;

	public Pagination() {
	}

	/**
	 * 构造器
	 * 
	 * @param pageNo
	 *            页码
	 * @param pageSize
	 *            每页几条数据
	 * @param totalCount
	 *            总共几条数据
	 */
	public Pagination(int pageNo, int pageSize, int totalCount) {
		super(pageNo, pageSize, totalCount);

	}

	/**
	 * 构造器
	 * 
	 * @param pageNo
	 *            页码
	 * @param pageSize
	 *            每页几条数据
	 * @param totalCount
	 *            总共几条数据
	 * @param list
	 *            分页内容
	 */
	public Pagination(int pageNo, int pageSize, int totalCount, List<T> list) {
		super(pageNo, pageSize, totalCount);
		this.list = list;
	}

	/**
	 * 第一条数据位置
	 * 
	 * @return
	 */
	public int getFirstResult() {
		return (pageNo - 1) * pageSize;
	}

	/**
	 * 当前页的数据
	 */
	private List<T> list;

	/**
	 * 当前页的分页样式
	 */
	private List<String> pageView;

	/**
	 * 获得分页内容
	 * 
	 * @return
	 */
	public List<T> getList() {
		return list;
	}

	/**
	 * 设置分页内容
	 * 
	 * @param list
	 */
	public void setList(List<T> list) {
		this.list = list;
	}

	/**
	 * 获得分页样式
	 * 
	 * @return
	 */
	public List<String> getPageView() {
		return pageView;
	}

	/**
	 * 设置分页样式
	 * 
	 * @param list
	 */
	public void setPageView(List<String> pageView) {
		this.pageView = pageView;
	}

	/**
	 * 分页显示样示部分
	 */
	public void pageView() {

		pageView = new ArrayList<String>();
		pageView.add("<div style='float: right;'><ul class='pagination'>");
		if (this.pageNo != 1) {
			pageView.add("<li><a href=\"javascript:void(0);\" onclick=\"goPage('1')\">首页</a>");
			pageView.add("<li><a href=\"javascript:void(0);\" onclick=\"goPage('" + (this.pageNo - 1) + "')\">上一页</a>");
		} else {
			pageView.add("<li class='disabled'><span>首页</span></li>");
			pageView.add("<li class='disabled'><span>上一页</span></li>");
		}

		if (this.getTotalPage() <= 10) {
			for (int i = 0; i < this.getTotalPage(); i++) {
				if ((i + 1) == this.pageNo) {
					pageView.add("<li class='active'><span>" + this.pageNo + "</span></li>");
					i = i + 1;
					if (this.pageNo == this.getTotalPage())
						break;
				}
				pageView.add("<li><a href=\"javascript:void(0);\" onclick=\"goPage('" + (i + 1) + "')\">" + (i + 1)
						+ "</a></li>");
			}
		} else if (this.getTotalPage() <= 20) {
			// 没有把...加上
			int l = 0;
			int r = 0;
			if (this.pageNo < 5) {
				l = this.pageNo - 1;
				r = 10 - l - 1;
			} else if (this.getTotalPage() - this.pageNo < 5) {
				r = this.getTotalPage() - this.pageNo;
				l = 10 - 1 - r;
			} else {
				l = 4;
				r = 5;
			}
			int tmp = this.pageNo - l;
			for (int i = tmp; i < tmp + 10; i++) {
				if (i == this.pageNo) {
					pageView.add("<li class='active'><span>" + this.pageNo + "</span></li>");
					i = i + 1;
					if (this.pageNo == this.getTotalPage())
						break;
				}
				pageView.add(
						"<li><a href=\"javascript:void(0);\" onclick=\"goPage('" + (i) + "')\">" + (i) + "</a></li>");
			}

		} else if (this.pageNo < 7) {
			for (int i = 0; i < 8; i++) {
				if (i + 1 == this.pageNo) {
					pageView.add("<li class='active'><span>" + this.pageNo + "</span></li>");
					i = i + 1;
				}
				pageView.add("<li><a href=\"javascript:void(0);\" onclick=\"goPage('" + (i + 1) + "')\">" + (i + 1)
						+ "</a></li>");
			}
			pageView.add("<li><span style='float: left'>...<span></li>");
			pageView.add("<li><a href=\"javascript:void(0);\" onclick=\"goPage('" + (this.getTotalPage() - 1) + "')\">"
					+ (this.getTotalPage() - 1) + "</a></li>");
			pageView.add("<li><a href=\"javascript:void(0);\" onclick=\"goPage('" + (this.getTotalPage()) + "')\">"
					+ (this.getTotalPage()) + "</a></li>");
		} else if (this.pageNo > this.getTotalPage() - 6) {

			pageView.add("<li><a href=\"javascript:void(0);\" onclick=\"goPage('" + (1) + "')\">" + (1) + "</a></li>");
			pageView.add("<li><a href=\"javascript:void(0);\" onclick=\"goPage('" + (2) + "')\">" + (2) + "</a></li>");
			pageView.add("<li><span style='float: left'>...<span></li>");
			for (int i = this.getTotalPage() - 8; i < this.getTotalPage(); i++) {
				if (i + 1 == this.pageNo) {
					pageView.add("<li class='active'><span>" + this.pageNo + "</span></li>");
					i = i + 1;
					if (this.pageNo == this.getTotalPage())
						break;
				}
				pageView.add("<li><a href=\"javascript:void(0);\" onclick=\"goPage('" + (i + 1) + "')\">" + (i + 1)
						+ "</a></li>");
			}
		} else {
			pageView.add("<li><a href=\"javascript:void(0);\" onclick=\"goPage('" + (1) + "')\">" + (1) + "</a></li>");
			pageView.add("<li><a href=\"javascript:void(0);\" onclick=\"goPage('" + (2) + "')\">" + (2) + "</a></li>");
			pageView.add("<li><span style='float: left'>...<span></li>");

			pageView.add("<li><a href=\"javascript:void(0);\" onclick=\"goPage('" + (this.pageNo - 2) + "')\">"
					+ (this.pageNo - 2) + "</a></li>");
			pageView.add("<li><a href=\"javascript:void(0);\" onclick=\"goPage('" + (this.pageNo - 1) + "')\">"
					+ (this.pageNo - 1) + "</a></li>");
			pageView.add("<li class='active'><span>" + this.pageNo + "</span></li>");
			pageView.add("<li><a href=\"javascript:void(0);\" onclick=\"goPage('" + (this.pageNo + 1) + "')\">"
					+ (this.pageNo + 1) + "</a></li>");
			pageView.add("<li><a href=\"javascript:void(0);\" onclick=\"goPage('" + (this.pageNo + 2) + "')\">"
					+ (this.pageNo + 2) + "</a></li>");

			pageView.add("<li><span style='float: left'>...<span></li>");
			pageView.add("<li><a href=\"javascript:void(0);\" onclick=\"goPage('" + (this.getTotalPage() - 1) + "')\">"
					+ (this.getTotalPage() - 1) + "</a></li>");
			pageView.add("<li><a href=\"javascript:void(0);\" onclick=\"goPage('" + (this.getTotalPage()) + "')\">"
					+ (this.getTotalPage()) + "</a></li>");
		}
		if (this.pageNo != this.getTotalPage()) {
			pageView.add(
					"<li><a href=\"javascript:void(0);\" onclick=\"goPage('" + (this.pageNo + 1) + "')\">下一页</a></li>");
			pageView.add("<li><a href=\"javascript:void(0);\" onclick=\"goPage('" + this.getTotalPage()
					+ "')\">尾页</a></li>");
		} else {
			pageView.add("<li class='disabled'><span>下一页</span></li>");
			pageView.add("<li class='disabled'><span>尾页</span></li>");
		}
		pageView.add("</ul></div>");
		pageView.add("<div  style='float: left;text-align: right;height: 30px;line-height: 30px;margin-top: 30px;font-size: 14px;' class='pagination'>共 "
				+ this.totalCount + " 条记录，共  " + this.getTotalPage() + " 页，当前为第 " + this.pageNo + " 页</div>");
		/*
		 * pageView.
		 * add("<div  style='float: left;text-align: right;margin: 20x 0;color: #777' class='pagination'>共 "
		 * +this.totalCount+" 条记录，共  " + this.getTotalPage() +
		 * " 页，当前为第 "+this.pageNo+" 页</div>");
		 */
	}
}
